package com.cloudsigns.factorymanager2.operate_maintain.activity;

import android.graphics.Color;
import android.os.Build;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.view.ViewPager;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.cloudsigns.factorymanager2.R;
import com.cloudsigns.factorymanager2.operate_maintain.fragment.OperateMaintainEmergenciesFramgent;
import com.cloudsigns.factorymanager2.operate_maintain.fragment.OperateMaintainMediaInspectionFragment;
import com.cloudsigns.factorymanager2.operate_maintain.fragment.OperateMaintainScheduleInformationFragment;
import com.cloudsigns.factorymanager2.produce.utils.ProduceForTeamEvent;
import com.cloudsigns.factorymanager2.project.adapter.ProjectMainViewPageAdapter;
import com.cloudsigns.factorymanager2.project.customview.CustomViewPager;
import com.cloudsigns.factorymanager2.project.customview.PageIndicatorView;

import java.util.ArrayList;

import de.greenrobot.event.EventBus;

public class OperateMaintainOnDutyFragmentActivity extends FragmentActivity implements View.OnClickListener {
    private RelativeLayout rl_back;
    private TextView tv_title;
    private View ll_schedule_information, ll_media_inspection, ll_emergencies;
    private TextView tv_schedule_information, tv_media_inspection, tv_emergencies;
    private CustomViewPager viewpager;
    private View rl_progressbar;
    public static final int SCHEDULE_INFORMATION = 0;
    public static final int MEDIA_INSPECTION = 1;
    public static final int EMERGENCIES = 2;
    public int currentIndex = SCHEDULE_INFORMATION;

    private OperateMaintainScheduleInformationFragment scheduleInformationFragment;
    private OperateMaintainMediaInspectionFragment mediaInspectionFragment;
    private OperateMaintainEmergenciesFramgent emergenciesFramgent;
    private ArrayList<Fragment> listFragments;
    private ProjectMainViewPageAdapter adapter;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        this.requestWindowFeature(Window.FEATURE_NO_TITLE);//去掉标题栏
        // 沉浸式状态栏
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            Window window = getWindow();
            window.clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
            window.getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN | View.SYSTEM_UI_FLAG_LAYOUT_STABLE);
            window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
            window.setStatusBarColor(Color.TRANSPARENT);
        } else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
            getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
        }
        setContentView(R.layout.layout_operate_maintain_on_duty);

        initView();

        tv_title.setText(getResources().getText(R.string.be_on_duty));
        EventBus.getDefault().register(this);// 注册

        initData();
    }

    private void initView() {
        rl_back = findViewById(R.id.rl_back);
        tv_title = findViewById(R.id.tv_title);
        rl_progressbar = findViewById(R.id.rl_progressbar);
        viewpager = findViewById(R.id.viewpager);
        ll_schedule_information = findViewById(R.id.ll_schedule_information);
        ll_media_inspection = findViewById(R.id.ll_media_inspection);
        ll_emergencies = findViewById(R.id.ll_emergencies);
        tv_schedule_information = findViewById(R.id.tv_schedule_information);
        tv_media_inspection = findViewById(R.id.tv_media_inspection);
        tv_emergencies = findViewById(R.id.tv_emergencies);

        rl_back.setOnClickListener(this);
        ll_schedule_information.setOnClickListener(this);
        ll_media_inspection.setOnClickListener(this);
        ll_emergencies.setOnClickListener(this);

        scheduleInformationFragment = new OperateMaintainScheduleInformationFragment();
        mediaInspectionFragment = new OperateMaintainMediaInspectionFragment();
        emergenciesFramgent = new OperateMaintainEmergenciesFramgent();

        listFragments = new ArrayList<Fragment>();
        listFragments.add(scheduleInformationFragment);
        listFragments.add(mediaInspectionFragment);
        listFragments.add(emergenciesFramgent);

        adapter = new ProjectMainViewPageAdapter(getSupportFragmentManager(), listFragments);
        viewpager.setAdapter(adapter);
        viewpager.setOffscreenPageLimit(2);

        PageIndicatorView indicator = findViewById(R.id.indicator);
        indicator.setViewPager(viewpager);
        indicator.setFades(false);
        indicator.setSelectedColor(getResources().getColor(R.color.rgb_2E75B6));
        indicator.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {

            @Override
            public void onPageSelected(int position) {
                showSelectedOption(position);
            }

            @Override
            public void onPageScrolled(int arg0, float arg1, int arg2) {
            }

            @Override
            public void onPageScrollStateChanged(int arg0) {

            }
        });
    }

    /**
     * 从服务器获取数据
     */
    private void initData() {

    }

    /**
     * 切换到哪个tab页
     *
     * @param pos
     */
    private void showSelectedOption(int pos) {
        // TODO Auto-generated method stub
        switch (pos) {
            // 排班信息
            case SCHEDULE_INFORMATION:
                ll_schedule_information.setSelected(true);
                ll_media_inspection.setSelected(false);
                ll_emergencies.setSelected(false);

                tv_schedule_information.setTextColor(getResources().getColor(R.color.rgb_3333333));
                tv_media_inspection.setTextColor(getResources().getColor(R.color.color_a2a2a4));
                tv_emergencies.setTextColor(getResources().getColor(R.color.color_a2a2a4));
                break;
            // 媒体巡查
            case MEDIA_INSPECTION:
                ll_schedule_information.setSelected(false);
                ll_media_inspection.setSelected(true);
                ll_emergencies.setSelected(false);

                tv_schedule_information.setTextColor(getResources().getColor(R.color.color_a2a2a4));
                tv_media_inspection.setTextColor(getResources().getColor(R.color.rgb_3333333));
                tv_emergencies.setTextColor(getResources().getColor(R.color.color_a2a2a4));
                break;
            // 突发事件
            case EMERGENCIES:
                ll_schedule_information.setSelected(false);
                ll_media_inspection.setSelected(false);
                ll_emergencies.setSelected(true);

                tv_schedule_information.setTextColor(getResources().getColor(R.color.color_a2a2a4));
                tv_media_inspection.setTextColor(getResources().getColor(R.color.color_a2a2a4));
                tv_emergencies.setTextColor(getResources().getColor(R.color.rgb_3333333));
                break;
            default:
                break;
        }
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.rl_back:
                finish();
                break;
            // 排班信息
            case R.id.ll_schedule_information:
                if (currentIndex != SCHEDULE_INFORMATION) {
                    currentIndex = SCHEDULE_INFORMATION;
                    viewpager.setCurrentItem(SCHEDULE_INFORMATION);
                    showSelectedOption(SCHEDULE_INFORMATION);
                }
                break;
            // 媒体巡查
            case R.id.ll_media_inspection:
                if (currentIndex != MEDIA_INSPECTION) {
                    currentIndex = MEDIA_INSPECTION;
                    viewpager.setCurrentItem(MEDIA_INSPECTION);
                    showSelectedOption(MEDIA_INSPECTION);
                }
                break;
            //　突发事件
            case R.id.ll_emergencies:
                if (currentIndex != EMERGENCIES) {
                    currentIndex = EMERGENCIES;
                    viewpager.setCurrentItem(EMERGENCIES);
                    showSelectedOption(EMERGENCIES);
                }
                break;
        }
    }

    public void onEventMainThread(ProduceForTeamEvent event) {

    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);//解注册
    }
}
